package com.example.aitoedu.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.aitoedu.constant.MessageConstant;
import com.example.aitoedu.dto.LoginEmailDTO;
import com.example.aitoedu.dto.RegisterDTO;
import com.example.aitoedu.entity.Chapter;
import com.example.aitoedu.entity.Student;
import com.example.aitoedu.entity.Teacher;
import com.example.aitoedu.service.ChapterService;
import com.example.aitoedu.service.StudentService;
import com.example.aitoedu.service.TeacherService;
import com.example.aitoedu.service.UserService;
import com.example.aitoedu.vo.LoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logic/user")
@Slf4j
@CrossOrigin
@Tag(name = "所有用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StudentService studentService;

    @Autowired
    private TeacherService teacherService;

    @PostMapping("/login/email")
    @Operation(summary = "邮箱登录")
    public SaResult login(@RequestBody LoginEmailDTO loginEmailDTO){
        log.info("邮箱登录：{}",loginEmailDTO);
        LoginVO loginVO = userService.loginEmail(loginEmailDTO);
        StpUtil.login(loginVO.getId());
        loginVO.setSatoken(StpUtil.getTokenValue());
        return SaResult.ok(MessageConstant.LOG_SUCCESS).setData(loginVO);
    }

    @GetMapping("/logout")
    @Operation(summary = "退出登录")
    public SaResult logout(){
        log.info("退出登录：{}",StpUtil.getTokenInfo());
        StpUtil.logout();
        return SaResult.ok(MessageConstant.LOGOUT);
    }

    @GetMapping("/certify")
    @Operation(summary = "发送验证码")
    public SaResult Certify(String email){
        log.info("需要发送验证码的邮箱：{}",email);
        userService.certify(email);
        return SaResult.ok(MessageConstant.CERTIFCATION_SUCCESS);
    }

    @PostMapping("/register")
    @Operation(summary = "注册")
    public SaResult Register(@RequestBody RegisterDTO registerDTO){
        log.info("注册信息：{}",registerDTO);
        userService.register(registerDTO);
        return SaResult.ok(MessageConstant.REGISTER_SUCCESS);
    }

    @GetMapping("/role")
    public SaResult Role(){
        return SaResult.ok().setData(userService.getRole());
    }

    @GetMapping("/KGK")
    @Operation(summary = "查询知识图谱")
    public SaResult KGK(Long courseId){
        List<Chapter> chapterList = chapterService.KGK(courseId);
        return SaResult.ok().setData(chapterList);
    }

    @GetMapping("student/pl")
    @Operation(summary = "个人信息")
    public SaResult stu_pl(){
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        Student student = (Student) redisTemplate.opsForValue().get("student_pl:"+id);
        if(student!=null) return SaResult.ok().setData(student);
        student = studentService.pl();
        redisTemplate.opsForValue().set("student_pl:"+id,student);
        return SaResult.ok().setData(student);
    }

    @GetMapping("teacher/pl")
    @Operation(summary = "个人信息")
    public SaResult tea_pl(){
        Long id = Long.valueOf(StpUtil.getLoginId().toString());
        Teacher teacher = (Teacher) redisTemplate.opsForValue().get("teacher_pl:"+id);
        if(teacher != null) return SaResult.ok().setData(teacher);
        teacher = teacherService.pl();
        redisTemplate.opsForValue().set("teacher_pl:"+id,teacher);
        return SaResult.ok().setData(teacher);
    }

    @GetMapping("/chapters")
    public SaResult chapters(Long courseId){
        List<Chapter> chapterList = userService.getChapters(courseId);
        return SaResult.ok().setData(chapterList);
    }
}
